﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace Google
{
    public partial class Form1 : Form
    {
        private HttpWebRequest request;
        private HttpWebResponse response;
        private StreamReader reader;
        private string client_id = "500592810242-14kscqt9hmoqmoaqcim2ftknc4pal9k6.apps.googleusercontent.com";
        private string client_secret = "RhSK5II9qAZB-ym6vOeNhuFz";
        private string redirect_uris = "http://localhost/oauth2callback";
        private string origion = "http://localhost/";
        private string scope = "https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile";
        private string access_token;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = "https://accounts.google.com/o/oauth2/auth?";
            url += "&scope=" + scope ;
            url += "&redirect_uri=" + redirect_uris;
            url += "&response_type=token";
            url += "&=" + client_id;
            webBrowser1.Url = new Uri(url);
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {

            //txt_url.Text = webBrowser1.Url.ToString();
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            txt_url.Text = webBrowser1.Url.ToString();
            if (txt_url.Text.Contains("http://localhost/oauth2callback"))
            {
                string temp_url = txt_url.Text;
                int token_index = temp_url.IndexOf("access_token");
                int type_index = temp_url.IndexOf("&token_type");
                if (token_index > 0 && type_index > 0)
                {
                    access_token = temp_url.Substring(token_index + 13,59);
                    MessageBox.Show("Access Token : " + access_token);
                    webBrowser1.Dispose();
                    //RequestUserInfo();
                    GetUserProfile();
                }
            }
        }

        void GetUserProfile()
        {
            try
            {
                string user_id = "100324033508059871213";
                request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/plus/v1/people/100324033508059871213?pp=1&key=AIzaSyBgoEdYCxsJMu8HaDcLv9rEd3offlIxPx4");
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
                response = (HttpWebResponse)request.GetResponse();
                reader = new StreamReader(response.GetResponseStream());
                string result = reader.ReadToEnd();
                MessageBox.Show("Result: " + result);
            }
            catch (Exception ex)
            {
                if (MessageBox.Show("Error : " + ex.Message + "\n\nDo you want to try again?", "Error Message", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.Retry)
                {
                    GetUserProfile();
                }
            }
        }

        void RequestUserInfo()
        {
            try
            {
                request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + access_token);
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
                response = (HttpWebResponse)request.GetResponse();
                reader = new StreamReader(response.GetResponseStream());
                string result = reader.ReadToEnd();
                MessageBox.Show("Result: " + result);
            }
            catch (Exception ex)
            {
                if (MessageBox.Show("Error : " + ex.Message + "\n\nDo you want to try again?", "Error Message", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.Retry)
                {
                    RequestUserInfo();
                }
            }
        }

        public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            GetUserProfile();
        }

    }
}
